package net.realsee.service.wcs.data.impl;

import cn.hutool.core.bean.BeanUtil;
import net.realsee.common.utils.StringUtils;
import net.realsee.domain.wcs.data.WcsCompany;
import net.realsee.domain.wcs.data.bo.WcsCompanyBo;
import net.realsee.domain.wcs.data.vo.WcsCompanyVo;
import net.realsee.framework.core.page.TableDataInfo;
import net.realsee.framework.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.realsee.mapper.wcs.data.WcsCompanyMapper;
import net.realsee.service.wcs.data.IWcsCompanyService;
import org.springframework.stereotype.Service;


import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 公司Service业务层处理
 *
 * @author realsee
 * @date 2023-08-16
 */
@RequiredArgsConstructor
@Service
public class WcsCompanyServiceImpl extends ServiceImpl<WcsCompanyMapper, WcsCompany> implements IWcsCompanyService {

    private final WcsCompanyMapper baseMapper;

    /**
     * 查询公司
     */
    @Override
    public WcsCompanyVo selectVoById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询公司列表
     */
    @Override
    public TableDataInfo<WcsCompanyVo> selectVoPage(WcsCompanyBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WcsCompany> lqw = buildVoQueryWrapper(bo);
        Page<WcsCompanyVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询公司列表
     */
    @Override
    public List<WcsCompanyVo> selectVoList(WcsCompanyBo bo) {
        LambdaQueryWrapper<WcsCompany> lqw = buildVoQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WcsCompany> buildVoQueryWrapper(WcsCompanyBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WcsCompany> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getCompany()), WcsCompany::getCompany, bo.getCompany());
        lqw.like(StringUtils.isNotBlank(bo.getName()), WcsCompany::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getFuid()), WcsCompany::getFuid, bo.getFuid());
        lqw.eq(bo.getTextId() != null, WcsCompany::getTextId, bo.getTextId());
        lqw.eq(bo.getRefrenceId() != null, WcsCompany::getRefrenceId, bo.getRefrenceId());
        lqw.eq(bo.getUnitId() != null, WcsCompany::getUnitId, bo.getUnitId());
        return lqw;
    }

    /**
     * 新增公司
     */
    @Override
    public Boolean insertByBo(WcsCompanyBo bo) {
        WcsCompany add = BeanUtil.toBean(bo, WcsCompany.class);
        return this.save(add);
    }

    /**
     * 修改公司
     */
    @Override
    public Boolean updateByBo(WcsCompanyBo bo) {
        WcsCompany update = BeanUtil.toBean(bo, WcsCompany.class);
        return this.updateById(update);
    }

    /**
     * 批量删除公司
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        return this.removeBatchByIds(ids);
    }
}
